# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

from tree_node import TreeNode


class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        return self.valid(root, float("-inf"), float("inf"))

    def valid(self,root, left, right):
        if root is None:
            return True

        if root.val >= right or root.val <= left:
            return False

        return self.valid(root.left, left, root.val) and self.valid(root.right, root.val, right)

        # if not root.left:
        # return True

        # if not root.right:
        # return True

        # if root.left:
        # if root.left.val < root.val:
        # if self.isValidBST(root.left):
        # return True
        # else:
        # return False

        # if root.right:
        # if root.right.val > root.val:
        # if self.isValidBST(root.right):
        # return True
        # else:
        # return False


def test_a():
    a = TreeNode(2)
    b = TreeNode(1)
    c = TreeNode(3)

    a.left = b
    a.right = c
    assert Solution().isValidBST(a) == True


def test_aa():
    a = TreeNode(1)
    b = TreeNode(2)
    c = TreeNode(3)

    a.left = b
    a.right = c
    assert Solution().isValidBST(a) == False


def test_has_not_right():
    a = TreeNode(1)
    b = TreeNode(-1)
    a.left = b
    assert Solution().isValidBST(a) == True


def test_left_not_less_than_right():
    a = TreeNode(1)
    b = TreeNode(1)
    a.left = b
    assert Solution().isValidBST(a) == False


def test_b():
    a = TreeNode(3)
    b = TreeNode(9)
    c = TreeNode(20)
    d = TreeNode(15)
    e = TreeNode(7)
    f = TreeNode(8)

    a.left = b
    a.right = c
    c.left = d
    c.right = e
    e.right = f

    assert Solution().isValidBST(a) == False


if __name__ == "__main__":
    a = TreeNode(2)
    b = TreeNode(1)
    c = TreeNode(3)

    a.left = b
    a.right = c
    print(Solution().isValidBST(a))
